package com.example.material

import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.android.material.snackbar.Snackbar

class ToolbarAndSoOnActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_toolbar)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        /**
         * Toolbar
         */
        setSupportActionBar(findViewById(R.id.toolbar_btn))

        /**
         * Floating Action Button
         */
        findViewById<FloatingActionButton>(R.id.fab).setOnClickListener { view ->
            /**
             * Snackbar
             */
            Snackbar.make(view, "Data deleted", Snackbar.LENGTH_LONG)
                .setAction("Undo") {
                    Toast.makeText(this, "Data restored", Toast.LENGTH_SHORT)
                        .show()
                }
                .show()
        }
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.toolbar, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.backup -> Toast.makeText(this, "Backup", Toast.LENGTH_SHORT)
                .show()

            R.id.delete -> Toast.makeText(this, "Delete", Toast.LENGTH_SHORT)
                .show()

            R.id.settings -> Toast.makeText(this, "Settings", Toast.LENGTH_SHORT)
                .show()
        }
        return true
    }

}